package com.bms.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bms.dao.PublisherDao;
import com.bms.entity.Publisher;

public class PublisherAction extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method=request.getParameter("method");
		PublisherDao dao=new PublisherDao();
		if("add".equals(method)){
			String name=request.getParameter("name");
			Publisher publisher=new Publisher(name);
			dao.insert(publisher);
			response.sendRedirect("publishers/index.jsp");
		}else if("delete".equals(method)){//删除
			String param=request.getParameter("id");
			int id=Integer.valueOf(param);
			dao.deletePublisherById(id);
			response.sendRedirect("publishers/index.jsp");
		}else if("doEdit".equals(method)){//完成修改
			String param=request.getParameter("id");
			int id=Integer.valueOf(param);
			String name=request.getParameter("name");
			Publisher publisher=new Publisher(id, name);
			dao.update(publisher);
			response.sendRedirect("publishers/index.jsp");
		}
	}

}
